import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import LoginView from '@/views/LoginView.vue'
import GrandParentView from '@/views/GrandParentView.vue'
import ElementDemoView from '@/views/ElementDemoView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/grand-parent',
    name: 'GrandParent',
    component: GrandParentView,
    beforeEnter: (to, from, next) => {
      console.log("grand-parent路由独享的守卫, %s => %s", from.path, to.path)
      next()
    }
  },
  {
    path: '/element-demo',
    name: 'ElementDemo',
    component: ElementDemoView
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log("全局前置守卫, %s => %s", from.path, to.path)
  next()
})

// 全局解析守卫
router.beforeResolve((to, from, next) => {
  console.log("全局解析守卫, %s => %s", from.path, to.path)
  next()
})

// 全局后置钩子
router.afterEach((to, from) => {
  console.log("全局后置钩子, %s => %s", from.path, to.path)
})

export default router
